<template>
  <a-modal
    :title="'特艺'"
    :visible="true"
    width="50%"
    :footer="null"
    @cancel="reset"
  >
    <div class="main">
      <a-card>
        <a-row
          type="flex"
          justify="start"
          align="top"
        >
          <a-col :span="24">
            <a-form
              :form="form"
              @submit="handleSubmit"
              :labelCol="formItemLayout.labelCol"
              :wrapperCol="formItemLayout.wrapperCol"
            >
              <a-form-item
                label="序号"
                :required="true"
              >
                <a-input
                  allow-clear
                  placeholder="请输入序号"
                  :maxLength="40"
                  v-decorator="[
                  `seq`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入序号'
                      }
                    ]
                  }
                ]"
                />
              </a-form-item>
              <a-form-item
                label="特艺"
                :required="true"
              >
                <a-select
                  allow-clear
                  :required="true"
                  show-search
                  placeholder="请选择特艺"
                  v-decorator="[
                  'specId',
                  {
                    rules: [{ required: true, message: '请选择特艺!' }]
                  }
                ]"
                >
                  <a-select-option
                    :value="item.id"
                    v-for="(item,index) in specList"
                    :key="index"
                  >
                    {{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
                <a-form-item
                label="销售色"
                :required="true"
              >
                <a-input
                  allow-clear
                  placeholder="请输入销售色"
                  :maxLength="40"
                  v-decorator="[
                  `styColorId`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入销售色'
                      }
                    ]
                  }
                ]"
                />
              </a-form-item>
              <a-form-item
                label="跳码"
                :required="true"
              >
                <a-input
                  allow-clear
                  placeholder="请输入跳码"
                  :maxLength="40"
                  v-decorator="[
                  `jumpSize`,
                  {
                    validateTrigger: ['change', 'blur'],
                    rules: [
                      {
                        required: true,
                        whitespace: true,
                        message: '请输入跳码'
                      }
                    ]
                  }
                ]"
                />
              </a-form-item>
              <a-form-item
                label="图片"
                :required="true"
              >
                <a-upload
                  name="file"
                  list-type="picture-card"
                  :multiple="true"
                  class="avatar-uploader"
                  :data="{ type: 1 }"
                  :action="uploadUrl"
                  :fileList="fileList"
                  @change="handleChange"
                  v-decorator="[
                  `imgurl`,
                  {
                    rules: [
                      {
                        required: true,
                        message: '请上传图片'
                      }
                    ]
                  }
                ]"
                >
                  <div>
                    <a-icon type="plus" />
                    <div class="ant-upload-text">
                      上传图片
                    </div>
                  </div>
                </a-upload>

              </a-form-item>
              <a-row>
                <a-col
                  :span="16"
                  :offset="8"
                  class="info-action"
                >
                  <a-button
                    type="primary"
                    @click="reset"
                  >返回</a-button>
                  <a-button
                    type="primary"
                    class="ml-20"
                    html-type="submit"
                    :loading="loading"
                  >保存</a-button>
                </a-col>
              </a-row>
            </a-form>
          </a-col>
        </a-row>
      </a-card>
    </div>

  </a-modal>
</template>

<script>
import { getDictChildList} from '@api/rd/index'
import { uploadUrl } from '@api/rd/index.js'


export default {
  components: {},

  props: {
    dict: {
      type: Object,
      default: null
    }
  },

  data() {
    return {
      id: '',
      uploadUrl,
      loading: false,
      idEdit: false,
      specList: [],
      craftBillSpec:{},
      fileList:[],
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          xl: { span: 5 },
          xs: { span: 5 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xl: { span: 10 },
          xs: { span: 16 },
          sm: { span: 16 }
        }
      }
    }
  },
  computed: {
    userInfo() {
      return this.$store.getters.userInfo || null
    }
  },
  created() {
    const { dict } = this.$props
    if (dict) {
      this.formTile = '编辑'
      this.id = dict.id
      this.idEdit = true
      this.$nextTick(() => {
        this.form.setFieldsValue({ name: dict.name, description: dict.description })
      })
    } else {
      this.formTile = '新增'
    }
    this.getSpecList()

  },
  methods: {
    reset() {
      this.form.resetFields()
      this.$emit('closeSea')
    },
    async getSpecList() {
      const params = {
        name: 'GY_SPEC'
      }
      try {
        const res = await getDictChildList('dict', params)
        if (res.code == 200) {
          this.specList = res.data
        } else {
          if (res.msg) {
            this.$message.error(res.msg)
          } else {
            this.$message.error('获取特艺列表失败！')
          }
        }
      } catch (error) {
        this.$message.error('获取特艺列表失败！')
      }
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.submit(values)
        }
      })
    },
    handleChange(info, item, index) {
      let fileList = [...info.fileList]
      this.fileList = [...fileList]
      if (info.file.status === 'uploading') {
        return
      }
      if (info.file.status === 'done') {
        let list = fileList.map(file => {
          if (file.response) {
            file.url = file.response.data.url
          }
          return file
        })
        this.fileList = list
      }
      if (info.file.status === 'error') {
        this.$message.error('图片上传失败')
      }
    },
    submit(values) {
      this.craftBillSpec = values
      this.craftBillSpec.imgurl = this.fileList[0].url
      this.$emit('closeSea', this.craftBillSpec)
    }
  }
}
</script>

<style scoped lang="less">
.info-action {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
  // margin-bottom: 20px;
}
.ml-20 {
  margin-left: 20px;
}
</style>
